package com.bulkmail.sender.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.bulkmail.sender.DataSession;
import com.bulkmail.sender.WriteXMLFile;
import com.bulkmail.sender.WriteXMLOptionFile;

public class SaveActionListener implements ActionListener {

	private MainFrame frame;

	public SaveActionListener(MainFrame frame) {
		this.frame = frame;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(DataSession.getZipPath());
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ZipOutputStream zos = new ZipOutputStream(fos);

		DataSession dataSession = new DataSession();
		dataSession.setNrEmails(Integer.valueOf(frame.noOfEmailTextField
				.getText()));
		dataSession.setSeconds(Integer.valueOf(frame.timeBetweenTextField
				.getText()));
		dataSession.setSmtp(frame.smtpRadioButton.isSelected());
		dataSession.setExs(frame.exchangeRadioButton.isSelected());
		dataSession.setAddCounter(frame.counterCheckBox.isSelected());
		dataSession.setAddRandomSubject(frame.randomCheckBox.isSelected());

		dataSession.setAttachPath(frame.attachmentTextField.getText());
		dataSession.setServer(frame.serverTextField.getText());
		dataSession.setPort(frame.portTextField.getText());
		dataSession.setUsername(frame.userTextField.getText());
		dataSession.setPassword(new String(frame.passwordField.getPassword()));

		dataSession.setFrom(frame.fromTextField.getText());
		List<String> temp = new ArrayList<String>();
		String[] toMany = frame.toTextField.getText().split(";");
		for (int i = 0; i < toMany.length; i++) {
			temp.add(toMany[i]);
		}
		dataSession.setTo(temp);
		dataSession.setSubject(frame.subjectTextField.getText());
		dataSession.setMessage(frame.messageArea.getText());

		WriteXMLFile writeSession = new WriteXMLFile(dataSession,
				DataSession.getMailXml());
		WriteXMLOptionFile writeOptions = new WriteXMLOptionFile(dataSession,
				DataSession.getOptXml());
		writeSession.writeXML();
		writeOptions.writeXMLOptionFile();

		String file1Name = DataSession.getMailXml();
		String file2Name = DataSession.getOptXml();

		try {
			addToZipFile(file1Name, zos);
			addToZipFile(file2Name, zos);

			zos.close();
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void addToZipFile(String fileName, ZipOutputStream zos)
			throws FileNotFoundException, IOException {

		File file = new File(fileName);
		System.out.println("Writing '" + file.getName() + "' to zip file");
		FileInputStream fis = new FileInputStream(file);

		ZipEntry zipEntry = new ZipEntry(file.getName());
		zos.putNextEntry(zipEntry);

		byte[] bytes = new byte[1024];
		int length;
		while ((length = fis.read(bytes)) >= 0) {
			zos.write(bytes, 0, length);
		}

		zos.closeEntry();
		fis.close();
	}
}